﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using GomeeSoft.src.classes;

namespace GomeeSoft.src.form.Popup
{
    public partial class PUAddUser : Form
    {
        public string UserID
        {
            get { return tb_user_id.Text; }
        }

        public PUAddUser()
        {
            InitializeComponent();
        }

        private void bt_save_ButtonClick(object sender, MouseEventArgs e)
        {
            if (tb_user_id.Text.Length == 0)
            {
                MessageBox.Show("Please input the user ID.");
                return;
            }

            if (tb_user_id.Text.Length < 4)
            {
                MessageBox.Show("The length o fUser ID must be larger than  4 letters");
                return;
            }

            if (tb_password.Text.Length == 0)
            {
                MessageBox.Show("Please input the password.");
                return;
            }

            if (tb_password2.Text.Length == 0)
            {
                MessageBox.Show("Please input the password check.");
                return;
            }

            if (tb_password.Text != tb_password2.Text)
            {
                MessageBox.Show("Password does not match.");
                return;
            }

            MD5 md5 = MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(tb_password.Text);
            byte[] hash = md5.ComputeHash(inputBytes);

            SQLiteQuery query = new SQLiteQuery();
            String sqlQuery = GomeeSoft.Properties.Resources.SQLUserAdd;
            query.SetCommand(sqlQuery);
            query.SetParameter("@user_id", tb_user_id.Text);
            query.SetParameter("@hash_password", hash);
            query.Execute();
            DialogResult = DialogResult.OK;
        }

        private void bt_cancel_ButtonClick(object sender, MouseEventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void PUAddUser_Paint(object sender, PaintEventArgs e)
        {
            Point[] p = new Point[3];
            p[0] = new Point(Width - 1, 0);
            p[1] = new Point(0, 0);
            p[2] = new Point(0, Height - 1);
            Pen pen = new Pen(Global.GetColor(Global.PageBorderLightColor[0]));
            e.Graphics.DrawLines(pen, p);
            p[0].Offset(-1, 1);
            p[1].Offset(1, 1);
            p[2].Offset(1, -1);
            pen = new Pen(Global.GetColor(Global.PageBorderLightColor[1]));
            e.Graphics.DrawLines(pen, p);
            p[0].Offset(-1, 1);
            p[1].Offset(1, 1);
            p[2].Offset(1, -1);
            pen = new Pen(Global.GetColor(Global.PageBorderLightColor[2]));
            e.Graphics.DrawLines(pen, p);
            p[0] = new Point(1, Height - 1);
            p[1] = new Point(Width - 1, Height - 1);
            p[2] = new Point(Width - 1, 1);
            pen = new Pen(Global.GetColor(Global.PageBorderDarkColor[0]));
            e.Graphics.DrawLines(pen, p);
            p[0].Offset(1, -1);
            p[1].Offset(-1, -1);
            p[2].Offset(-1, 1);
            pen = new Pen(Global.GetColor(Global.PageBorderDarkColor[1]));
            e.Graphics.DrawLines(pen, p);
            p[0].Offset(1, -1);
            p[1].Offset(-1, -1);
            p[2].Offset(-1, 1);
            pen = new Pen(Global.GetColor(Global.PageBorderDarkColor[2]));
            e.Graphics.DrawLines(pen, p);
            pen = new Pen(Global.GetColor(Global.PopupWindowTitleColor));
            SolidBrush b = new SolidBrush(Global.GetColor(Global.PopupWindowTitleColor));
            p[0] = new Point(3, 3);
            Size s = new Size(Width - 7, Height - 7);
            e.Graphics.DrawRectangle(pen, new Rectangle(p[0], s));
            s = new Size(Width - 7, 21);
            e.Graphics.FillRectangle(b, new Rectangle(p[0], s));
            pen.Dispose();            
        }
    }
}
